package com.dongdong.bookstore.module.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ums.model.ResourceCategory;
import com.dongdong.bookstore.module.ums.mapper.ResourceCategoryMapper;
import com.dongdong.bookstore.module.ums.service.ResourceCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongdong.bookstore.module.ums.viewmodel.ResourceCategoryParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资源分类表  服务实现类
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-20
 */
@Service
public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMapper, ResourceCategory> implements ResourceCategoryService {

    @Override
    public List<ResourceCategory> listAll() {
        QueryWrapper<ResourceCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(ResourceCategory::getSort);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean create(ResourceCategoryParam resourceCategoryParam) {
        ResourceCategory resourceCategory = new ResourceCategory();
        BeanUtils.copyProperties(resourceCategoryParam, resourceCategory);
        return save(resourceCategory);
    }

    @Override
    public boolean update(Long id, ResourceCategoryParam resourceCategoryParam) {
        ResourceCategory category = new ResourceCategory();
        BeanUtils.copyProperties(resourceCategoryParam, category);
        category.setId(id);
        return updateById(category);
    }
}
